import { SelectQueryBuilder } from 'typeorm';
import { PageQuery, PageResult } from '../interfaces/Pagination';

export async function BuildPagination<T>(query: SelectQueryBuilder<T>, params: PageQuery): Promise<PageResult<T>> {
  const { page = 1, limit = 10 } = params;
  const totalCount = await query.getCount();
  query = query.skip((page - 1) * limit).take(limit);
  return {
    totalCount,
    list: await query.getMany(),
  };
}
